let dom = require("../../utils/dom.js");
require("../../assets/css/index.less");

// 2.引入当前样式表
require("../../assets/fonts/iconfont.css");
require("./dyn.less");
let https = require("../../utils/https.js");
let local = require("../../utils/local.js");
// import "weui";
require("../../assets/lib/weui/weui.css");
const weui = require("../../assets/lib/weui/weui.js");

dom.ready(function () {
  dom.addEvent(".close-btn", function () {
    location.href = "circle.html";
  });
  let id = 0;
  let arr = []; // 存储url地址
  dom.getEle("#updata-img").addEventListener("change", function () {
    // console.log(1);
    // let picList = document.querySelector('#picList')
    // let img = document.createElement('img')
    // img.id = "chenga-img"
    dom.$("label").previousElementSibling.innerHTML += `

           <div class="set-img">
             <img src="#" id="chenga-img" />
           </div>
    `;
    let f = this.files[0];
    let fd = new FormData();
    fd.append("file", f);

    https.post("/api/circles/uploadImg", fd, (res) => {
      console.log(res.data.url);
      arr.push(res.data.url);

      if (dom.$("#chenga-img").length > 0) {
        dom.$("#chenga-img")[++id].src = https.BASE_URL + res.data.url;
      } else {
        dom.$("#chenga-img").src = https.BASE_URL + res.data.url;
      }
    });
    // picList.appendChild(img);
  });
  //选择省份
  let provinceInfo;
  let cityId;
  dom.addEvent("#showProvince", function () {
    // console.log("12312312");
    // //获取所有省份
    https.get("/api/shared/province", function (res) {
      // console.log(res);
      //判定
      if (res.errno == 0) {
        // console.log(res.data);
        //语法: weui.picker()
        //注意: picker(数据,对象)
        //数据格式必需是: [{label:文本,value:值}]
        //处理数据
        let proData = res.data.map((v) => {
          // console.log(v);
          return {
            label: v.name,
            value: v.id,
          };
        });
        weui.picker(proData, {
          onConfirm: (result) => {
            dom.getEle("#showProvince").innerHTML = result[0].label;
            //存当前省份信息
            provinceInfo = result[0];
          },
          title: "请选择省份",
        });
      }
    });
  });

  //选择城市
  dom.addEvent("#showCity", function () {
    if (provinceInfo) {
      //获取所有省份
      https.get(
        "/api/shared/city?provinceId=" + provinceInfo.value,
        function (res) {
          // console.log(res);
          //判定
          if (res.errno == 0) {
            let proData = res.data.map((v) => {
              // console.log(v);
              return {
                label: v.name,
                value: v.id,
              };
            });
            weui.picker(proData, {
              onConfirm: (result) => {
                dom.getEle("#showCity").innerHTML = result[0].label;
                cityId = result[0];
              },
              title: "请选择城市",
            });
          }
        }
      );
    } else {
      weui.alert("请先选择城市");
    }
  });

  //  发送动态
  //  内容
  // 发布省份ID
  // 发布城市ID
  // 图片数组

  dom.addEvent(".send", function () {
    if (!provinceInfo || !cityId) {
      weui.alert("请选择省市");
    } else {
      https.post(
        "/api/circles/opinion",
        {
          content: dom.$("#text-content").value || " ",
          releaseProvinceId: provinceInfo.value,
          releaseCityId: cityId.value,
          imgUrls: arr,
        },
        (res) => {
          console.log(res);
          location.href = "circle.html";
        }
      );
    }
  });

  dom.$("textarea").addEventListener("input", function () {
    console.log(arr);
    if (this.value.length > "140") {
      this.value = this.value.slice(0, 140);
      dom.$(".sum").innerHTML = 140;
      dom.$(".count-word").style.color = "red";
    } else {
      dom.$(".count-word").style.color = "black";
      dom.$(".sum").innerHTML = this.value.length;
    }
  });
});
